package taskit.logic;

import java.util.Stack;

import taskit.datastructures.Command;
import taskit.test.Log;

/**
 * TimeMachine class is for the Undo/Redo functions of Parser
 * @author Joseph Matthias Goh
 */
// @author A0087072A
public class TimeMachine {
	private Stack<Command> UndoHistory = new Stack<Command>();
	private Stack<Command> RedoHistory = new Stack<Command>();
	
	/**
	 * Pops the last Command from the Undo stack
	 * @return the last Command in Undo
	 */
	public Command popFromUndo() {
		try {
			return UndoHistory.pop();
		} catch (Exception anyEx) {
			Log.add("popFromRedo(); exception");
			anyEx.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Pops the last Command from the Redo stack
	 * @return the last Command in Redo
	 */
	public Command popFromRedo() {
		try {
			return RedoHistory.pop();
		} catch (Exception anyEx) {
			Log.add("popFromRedo(); exception");
			anyEx.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Pushes a command to the Undo stack
	 * @param command command to be pushed
	 * @return 0 if successful, 1 otherwise
	 */
	public int pushToUndo(Command command) {
		try {
			UndoHistory.push(command);
		} catch(Exception anyEx) {
			Log.add("pushToUndo() exception");
			anyEx.printStackTrace();
			return 1;
		}
		return 0;
	}
	
	/**
	 * Pushes a Command to the Redo stack
	 * @param command command to be pushed
	 * @return 0 if successful, 1 otherwise
	 */
	public int pushToRedo(Command command) {
		try {
			RedoHistory.push(command);
		} catch(Exception anyEx) {
			Log.add("pushToRedo() exception");
			anyEx.printStackTrace();
			return 1;
		}
		return 0;
	}
	
	/**
	 * Clears the Redo stack, this should be done after
	 * every action that is not an undo operation
	 * @return 0 if successful, 1 otherwise
	 */
	public int clearRedo() {
		try {
			RedoHistory.clear();
		} catch(Exception anyEx) {
			Log.add("clearRedo() exception");
			anyEx.printStackTrace();
			return 1;
		}
		return 0;
	}
	
	/**
	 * Checks if the undo stack is empty
	 * @return true if undo stack is empty, false otherwise
	 */
	public boolean undoIsEmpty() {
		return UndoHistory.empty();
	}
	
	/**
	 * Checks if the redo stack is empty
	 * @return true if redo stack is empty, false otherwise
	 * @return
	 */
	public boolean redoIsEmpty() {
		return RedoHistory.empty();
	}
}
